﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ConnectToDatabase;
using ConnectToDatabase.Model;

namespace QLGiayDep.Forms
{
    /// <summary>
    /// Interaction logic for FormThemDauSP.xaml
    /// </summary>
    public partial class FormDauSP : Page
    {
        private UpdateDB updateDB;
        private SelectDB selectDB;
        private InsertDB insertDB;
        private bool m_bSelectData = false;
        string URL;

        public FormDauSP(string _URL)
        {
            
                InitializeComponent();
                URL = _URL;
                selectDB = (SelectDB)Activator.GetObject(typeof(SelectDB), URL + "SELECTDB_OBJECT");
                insertDB = (InsertDB)Activator.GetObject(typeof(InsertDB), URL + "INSERTDB_OBJECT");

                dataDauSP.ItemsSource = selectDB.SelectAllDauSanPham(1);
                ShowComboboxLoaiSP(cmbLoaiSP, 1);
                ShowComboboxNhaPP(cmbNhaPP, 1);
        }

        public void ShowComboboxLoaiSP(ComboBox cmb, int TinhTrang)
        {
            for (int i = 0; i < selectDB.SelectAllLoaiSanPham(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllLoaiSanPham(TinhTrang)[i].MaLoaiSanPham.ToString());
            }
        }

        public void ShowComboboxNhaPP(ComboBox cmb, int TinhTrang)
        {
            for (int i = 0; i < selectDB.SelectAllNhaPhanPhoi(TinhTrang).Count; i++)
            {
                cmb.Items.Add(selectDB.SelectAllNhaPhanPhoi(TinhTrang)[i].MaNhaPP.ToString());
            }
        }

        private void btnThemDSP_Click(object sender, RoutedEventArgs e)
        {
            WinThemDauSP m_wThemDauSP = new WinThemDauSP(URL);
            m_wThemDauSP.Show();
        }

        private void btnCapNhatDSP_Click(object sender, RoutedEventArgs e)
        {
            //showDB.ShowListDauSanPham(dataDauSP, 1);
            dataDauSP.ItemsSource = selectDB.SelectAllDauSanPham(1);
        }

        private void dataDauSP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!m_bSelectData)
            {
                var item = dataDauSP.SelectedItem as DauSanPham;
                txtMaDauSP.Text = item.MaDauSanPham.ToString();
                txtTenDauSP.Text = item.TenDauSanPham.ToString();
                txtHanBan.Text = item.ThoiHanBan.ToString();
                txtGiaBan.Text = item.GiaBan.ToString();
                cmbLoaiSP.SelectedItem = item.MaLoaiSanPham.ToString();
                cmbNhaPP.SelectedItem = item.MaNhaPP.ToString();
            }
            else
            {
                m_bSelectData = false;
                
            }
        }

        private void btnSuaDSP_Click(object sender, RoutedEventArgs e)
        {
            updateDB = new UpdateDB();
            updateDB.UpdateDauSanPham(txtMaDauSP.Text, cmbLoaiSP.SelectedItem.ToString(), txtTenDauSP.Text, cmbNhaPP.SelectedItem.ToString(),
                                      Int32.Parse(txtHanBan.Text), Int32.Parse(txtGiaBan.Text), 1);
            m_bSelectData = true;
            dataDauSP.ItemsSource = selectDB.SelectAllDauSanPham(1);
        }
    }
}
